package com.uniflow.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 流程实例实体
 */
public class ProcessInstance {
    
    private String id;
    private String processDefinitionId;
    private String processDefinitionKey;
    private String processDefinitionName;
    private Integer processDefinitionVersion;
    private String deploymentId;
    private String businessKey;
    private String name;
    private String description;
    private String startUserId;
    private String startUserName;
    private String tenantId;
    private boolean suspended;
    private boolean ended;
    private String deleteReason;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;
    
    private Long durationInMillis;
    
    // 扩展字段
    private Map<String, Object> variables; // 流程变量
    private String currentActivityId; // 当前活动节点ID
    private String currentActivityName; // 当前活动节点名称
    private Integer activeTaskCount; // 活跃任务数量
    private String status; // 流程状态：running, completed, suspended, terminated
    
    // 构造函数
    public ProcessInstance() {}
    
    public ProcessInstance(String id, String processDefinitionKey, String businessKey) {
        this.id = id;
        this.processDefinitionKey = processDefinitionKey;
        this.businessKey = businessKey;
    }
    
    // Getters and Setters
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getProcessDefinitionId() {
        return processDefinitionId;
    }
    
    public void setProcessDefinitionId(String processDefinitionId) {
        this.processDefinitionId = processDefinitionId;
    }
    
    public String getProcessDefinitionKey() {
        return processDefinitionKey;
    }
    
    public void setProcessDefinitionKey(String processDefinitionKey) {
        this.processDefinitionKey = processDefinitionKey;
    }
    
    public String getProcessDefinitionName() {
        return processDefinitionName;
    }
    
    public void setProcessDefinitionName(String processDefinitionName) {
        this.processDefinitionName = processDefinitionName;
    }
    
    public Integer getProcessDefinitionVersion() {
        return processDefinitionVersion;
    }
    
    public void setProcessDefinitionVersion(Integer processDefinitionVersion) {
        this.processDefinitionVersion = processDefinitionVersion;
    }
    
    public String getDeploymentId() {
        return deploymentId;
    }
    
    public void setDeploymentId(String deploymentId) {
        this.deploymentId = deploymentId;
    }
    
    public String getBusinessKey() {
        return businessKey;
    }
    
    public void setBusinessKey(String businessKey) {
        this.businessKey = businessKey;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getStartUserId() {
        return startUserId;
    }
    
    public void setStartUserId(String startUserId) {
        this.startUserId = startUserId;
    }
    
    public String getStartUserName() {
        return startUserName;
    }
    
    public void setStartUserName(String startUserName) {
        this.startUserName = startUserName;
    }
    
    public String getTenantId() {
        return tenantId;
    }
    
    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }
    
    public boolean isSuspended() {
        return suspended;
    }
    
    public void setSuspended(boolean suspended) {
        this.suspended = suspended;
    }
    
    public boolean isEnded() {
        return ended;
    }
    
    public void setEnded(boolean ended) {
        this.ended = ended;
    }
    
    public String getDeleteReason() {
        return deleteReason;
    }
    
    public void setDeleteReason(String deleteReason) {
        this.deleteReason = deleteReason;
    }
    
    public LocalDateTime getStartTime() {
        return startTime;
    }
    
    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }
    
    public LocalDateTime getEndTime() {
        return endTime;
    }
    
    public void setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
    }
    
    public Long getDurationInMillis() {
        return durationInMillis;
    }
    
    public void setDurationInMillis(Long durationInMillis) {
        this.durationInMillis = durationInMillis;
    }
    
    public Map<String, Object> getVariables() {
        return variables;
    }
    
    public void setVariables(Map<String, Object> variables) {
        this.variables = variables;
    }
    
    public String getCurrentActivityId() {
        return currentActivityId;
    }
    
    public void setCurrentActivityId(String currentActivityId) {
        this.currentActivityId = currentActivityId;
    }
    
    public String getCurrentActivityName() {
        return currentActivityName;
    }
    
    public void setCurrentActivityName(String currentActivityName) {
        this.currentActivityName = currentActivityName;
    }
    
    public Integer getActiveTaskCount() {
        return activeTaskCount;
    }
    
    public void setActiveTaskCount(Integer activeTaskCount) {
        this.activeTaskCount = activeTaskCount;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    /**
     * 获取流程持续时间（格式化）
     */
    public String getFormattedDuration() {
        if (durationInMillis == null) {
            return null;
        }
        
        long seconds = durationInMillis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long days = hours / 24;
        
        if (days > 0) {
            return days + "天" + (hours % 24) + "小时" + (minutes % 60) + "分钟";
        } else if (hours > 0) {
            return hours + "小时" + (minutes % 60) + "分钟";
        } else if (minutes > 0) {
            return minutes + "分钟" + (seconds % 60) + "秒";
        } else {
            return seconds + "秒";
        }
    }
    
    /**
     * 判断流程是否正在运行
     */
    public boolean isRunning() {
        return !ended && !suspended;
    }
    
    @Override
    public String toString() {
        return "ProcessInstance{" +
                "id='" + id + '\'' +
                ", processDefinitionKey='" + processDefinitionKey + '\'' +
                ", businessKey='" + businessKey + '\'' +
                ", startUserId='" + startUserId + '\'' +
                ", suspended=" + suspended +
                ", ended=" + ended +
                '}';
    }
}